package cn.tedu.ivos.user.service.impl;

import cn.tedu.ivos.base.exception.ServiceException;
import cn.tedu.ivos.base.response.StatusCode;
import cn.tedu.ivos.user.mapper.UserMapper;
import cn.tedu.ivos.user.entity.User;
import cn.tedu.ivos.user.service.UserService;
import cn.tedu.ivos.user.pojo.dto.UserLoginParam;
import cn.tedu.ivos.user.pojo.vo.UserVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.rmi.server.ServerCloneException;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;
    @Override
    public UserVO login(UserLoginParam userLoginParam) {
        UserVO userVO = new UserVO();
        User user=userMapper.selectByUsernameAndPassword(userLoginParam);
        if (user ==null){
            throw new ServiceException(StatusCode.USERNAME_ERROR);
        }
        BeanUtils.copyProperties(user,userVO);
        log.info("uservo{}",userVO);
        return userVO;
    }

    @Override
    public List<UserVO> list() {
        List<User> list =userMapper.list();
        return list.stream()
                .map(user ->{
                    UserVO userVO = new UserVO();
                    BeanUtils.copyProperties(user,userVO);
                    return userVO;
                }).collect(Collectors.toList());
    }
}
